package by.grsu.pereverzev.page;

import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.text.DateFormatter;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.pereverzev.app.PereverzevWebApplication;
import by.grsu.pereverzev.dao.impl.FlightDao;
import by.grsu.pereverzev.entity.Passenger;
import by.grsu.pereverzev.entity.Flight;

public class FlightListPage extends WebPage {
	private final FlightDao flightDao;
	private static final SimpleDateFormat sdf = new SimpleDateFormat("DD.mm.yyyy HH:MM");
	
	public FlightListPage() {
		super();
		flightDao = new FlightDao(PereverzevWebApplication.DATA_XML_FOLDER);
		someshit(flightDao.getAll());
	}
	
	public FlightListPage(List<Flight> flights) {
		super();
		flightDao = new FlightDao(PereverzevWebApplication.DATA_XML_FOLDER);
		someshit(flights);
	}
	
	private void someshit(final List<Flight> flights) {
		add(new DataView<Flight>("table-rows", new ListDataProvider<Flight>(flights)) {
			@Override
			protected void populateItem(final Item<Flight> item) {
				final Flight flight = item.getModelObject();

				item.add(new Label("id", flight.getId()));
				item.add(new Label("destStation", flight.getDestStation()));
				item.add(new Label("dateOfDeparture", sdf.format(flight.getDateOfDeparture())));
				item.add(new Label("cost", flight.getCost()));
					
					

				item.add(new Link("orderLink") {
					@Override
					public void onClick() {
						setResponsePage(new FlightOrderPage(flight));
					}
				});
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new FlightEditPage(flight));
					}
				});

				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						flightDao.delete(flight.getId());
						setResponsePage(new FlightListPage());
					}
				});
			}
		});
	}
	
}
